/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managerBeanAdmin;

import entity.Department;
import entity.Members;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import managerBeanViews.Login;
import processor.DepartmentFacade;
import processor.MembersFacade;


@ManagedBean(name = "memberBean")
@SessionScoped
public class MemberBean implements Serializable {

    @EJB
    private DepartmentFacade departmentFacade;
    @EJB
    private MembersFacade membersFacade;
    @ManagedProperty(value = "#{login}")//this is EL name of your bean 
    private Login injectedBean;
    private int memberID;
    private int departmentID;
    private String memberName;
    private String memberUser;
    private String memberGender;
    private String memberAddress;
    private String memberPhone;
    private String memberPw;
    private String memberPosistion;
    private int memberRole;
    private String departmentName;
    private Members m = new Members();
    private List<Members> list = new ArrayList<Members>();
    private List<Members> filterMembers;
    private List<Members> technicians;
    private Members memberSelected;

    //Function Display all Members
    public List<Members> allMembers() {
        List<Members> findAll = membersFacade.findAll();
        return findAll;
    }

    public String forwardEdit() {
        setM(getMemberSelected());
        return "editMembers.xhtml";
    }

    public void addMembers() {
        FacesMessage msg;
        try {
            if (membersFacade.getMembersByMembersUser(getMemberUser()) == null) {
                Department dep = departmentFacade.getDepartment(getDepartmentID());
                Members mem = new Members();
                mem.setMembersName(getMemberName());
                mem.setMembersUser(getMemberUser());
                mem.setMembersPassword(getMemberPw());
                mem.setMembersGender(getMemberGender());
                mem.setMembersAddress(getMemberAddress());
                mem.setMembersPhone(getMemberPhone());
                mem.setMembersRole(getMemberRole());
                mem.setMembersPosition(getMemberPosistion());
                
                mem.setDepartmentID(dep);
                membersFacade.create(mem);
                msg = new FacesMessage("Created member: ", getMemberName() + " successfully.");
            } else {
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN,"Member: ", getMemberUser() + " is existed.");
            }
        } catch (Exception e) {
            msg = new FacesMessage("Error: ", e.getMessage());
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void editMembers(ActionEvent actionEvent) {
        FacesMessage msg;
        Department dep;
        try {
            // get department of the member
            if (getM().getDepartmentID() == null || getM().getDepartmentID().getDepartmentID() != getDepartmentID()) {
                dep = departmentFacade.getDepartment(getDepartmentID());
            } else {
                dep = getM().getDepartmentID();
            }
            //edit information of the member
            if (membersFacade.editMembers(getM().getMembersID(),
                    getM().getMembersName(),
                    getM().getMembersUser(),
                    getM().getMembersGender(),
                    getM().getMembersAddress(),
                    getM().getMembersPhone(),
                    getM().getMembersPassword(),
                    getM().getMembersPosition(),
                    getM().getMembersRole(),
                    dep)) {
                msg = new FacesMessage("Edited member: ", getM().getMembersName() + " successfully.");
            } else {
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN,"Edited member: ", getM().getMembersName() + " failed.");
            }
        } catch (Exception e) {
            msg = new FacesMessage("Error: ", e.getMessage());
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    //Function delete Members:
    public void deleteMember(ActionEvent actionEvent) {
        membersFacade.remove(getMemberSelected());
    }

    public String getErrorMessage(final String clientId) {
        if (clientId.equals("form:membersName")) {
            return "Name is required and betwwen 3 to 200 letters";
        } else if (clientId.equals("form:membersUser")) {
            return "User is required and betwwen 3 to 200 letters";
        } else if (clientId.equals("form:membersPassword")) {
            return "Password must have at least one digit,one upper letter,one lower letter and greate than six letters";
        } else if (clientId.equals("form:membersPhone")) {
            return "Phone must have between 6 to 20 digits";
        } else if (clientId.equals("form:membersPosition")) {
            return "Position is required and betwwen 3 to 200 letters";
        } else {
            Iterator<FacesMessage> iter = FacesContext.getCurrentInstance().getMessages(clientId);
            if (iter.hasNext()) {
                return iter.next().getDetail(); // or getSummary()
            }
            return "";
        }
    }

    /**
     * Creates a new instance of MemberBean
     */
    public MemberBean() {
    }

    /**
     * @return the memberID
     */
    public int getMemberID() {
        return memberID;
    }

    /**
     * @param memberID the memberID to set
     */
    public void setMemberID(int memberID) {
        this.memberID = memberID;
    }

    /**
     * @return the departmentID
     */
    public int getDepartmentID() {
        return departmentID;
    }

    /**
     * @param departmentID the departmentID to set
     */
    public void setDepartmentID(int departmentID) {
        this.departmentID = departmentID;
    }

    /**
     * @return the memberName
     */
    public String getMemberName() {
        return memberName;
    }

    /**
     * @param memberName the memberName to set
     */
    public void setMemberName(String memberName) {
        this.memberName = memberName;
    }

    /**
     * @return the memberUser
     */
    public String getMemberUser() {
        return memberUser;
    }

    /**
     * @param memberUser the memberUser to set
     */
    public void setMemberUser(String memberUser) {
        this.memberUser = memberUser;
    }

    /**
     * @return the memberGender
     */
    public String getMemberGender() {
        return memberGender;
    }

    /**
     * @param memberGender the memberGender to set
     */
    public void setMemberGender(String memberGender) {
        this.memberGender = memberGender;
    }

    /**
     * @return the memberAddress
     */
    public String getMemberAddress() {
        return memberAddress;
    }

    /**
     * @param memberAddress the memberAddress to set
     */
    public void setMemberAddress(String memberAddress) {
        this.memberAddress = memberAddress;
    }

    /**
     * @return the memberPhone
     */
    public String getMemberPhone() {
        return memberPhone;
    }

    /**
     * @param memberPhone the memberPhone to set
     */
    public void setMemberPhone(String memberPhone) {
        this.memberPhone = memberPhone;
    }

    /**
     * @return the memberPw
     */
    public String getMemberPw() {
        return memberPw;
    }

    /**
     * @param memberPw the memberPw to set
     */
    public void setMemberPw(String memberPw) {
        this.memberPw = memberPw;
    }

    /**
     * @return the memberPosistion
     */
    public String getMemberPosistion() {
        return memberPosistion;
    }

    /**
     * @param memberPosistion the memberPosistion to set
     */
    public void setMemberPosistion(String memberPosistion) {
        this.memberPosistion = memberPosistion;
    }

    /**
     * @return the memberRole
     */
    public int getMemberRole() {
        return memberRole;
    }

    /**
     * @param memberRole the memberRole to set
     */
    public void setMemberRole(int memberRole) {
        this.memberRole = memberRole;
    }

    /**
     * @return the departmentName
     */
    public String getDepartmentName() {
        return departmentName;
    }

    /**
     * @param departmentName the departmentName to set
     */
    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    /**
     * @return the m
     */
    public Members getM() {
        return m;
    }

    /**
     * @param m the m to set
     */
    public void setM(Members m) {
        this.m = m;
    }
  
    /**
     * @return the list
     */
    public List<Members> getList() {
        
        list = membersFacade.getMembersAll(injectedBean.getCustomer().getMembersRole());
        return list;
    }

    /**
     * @param list the list to set
     */
    public void setList(List<Members> list) {
        this.list = list;
    }

    /**
     * @return the filterMembers
     */
    public List<Members> getFilterMembers() {
        return filterMembers;
    }

    /**
     * @param filterMembers the filterMembers to set
     */
    public void setFilterMembers(List<Members> filterMembers) {
        this.filterMembers = filterMembers;
    }

    /**
     * @return the technicians
     */
    public List<Members> getTechnicians() {
        technicians = membersFacade.getTechnicianAll();
        return technicians;
    }

    /**
     * @param technicians the technicians to set
     */
    public void setTechnicians(List<Members> technicians) {
        this.technicians = technicians;
    }
    
    /**
     * @return the memberSelected
     */
    public Members getMemberSelected() {
        return memberSelected;
    }

    /**
     * @param memberSelected the memberSelected to set
     */
    public void setMemberSelected(Members memberSelected) {
        this.memberSelected = memberSelected;
    }

    /**
     * @return the injectedBean
     */
    public Login getInjectedBean() {
        return injectedBean;
    }

    /**
     * @param injectedBean the injectedBean to set
     */
    public void setInjectedBean(Login injectedBean) {
        this.injectedBean = injectedBean;
    }
}
